<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2021/1/4
 * Time: 15:23
 */
namespace App\Http\Controllers\Admin;

use App\Constant\ErrorConstant;
use App\Service\Admin\AreaService;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;

class AreaController extends BaseController
{
    protected $service;

    public function __construct(Request $request, AreaService $service)
    {
        parent::__construct($request);

        $this->service  =   $service;
    }

    /**
     * 盒子地区列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function listArea()
    {
        $limit  =   (int) $this->request->input('page_size', NULL);
        $page   =   (int) $this->request->input('page', 1);
        [$flag, $result]    =   $this->service->areaList($limit, $page);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::SYSTEM_AREA_LIST_ERROR, $result);
        }
    }

    /**
     * 盒子店铺列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function shopArea()
    {
        $limit  =   (int) $this->request->input('page_size', NULL);
        $page   =   (int) $this->request->input('page', 1);
        [$flag, $result]    =   $this->service->areaShop($limit, $page);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::SYSTEM_AREA_SHOP_ERROR, $result);
        }
    }

    /**
     * 关联店铺用户列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function connectArea()
    {
        $limit  =   (int) $this->request->input('page_size', NULL);
        $page   =   (int) $this->request->input('page', 1);
        $shop_id=   (int) $this->request->post('shop_id', 1);
        [$flag, $result]    =   $this->service->areaConnect($shop_id, $limit, $page);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::SYSTEM_AREA_CONNECT_ERROR, $result);
        }
    }

    /**
     * 店铺用户绑定
     * @return \Illuminate\Http\JsonResponse
     */
    public function bindArea()
    {
        $validator  =   Validator::make($this->request->post(), [
            'owner_id'  =>  'required|integer|exists:mini_box_owner,id',
            'shop_id'   =>  'required|integer|exists:mini_shop,id',
            'user_id'   =>  'required|integer|exists:mini_user,uid',
        ], [
            'owner_id.required' =>  '盒子用户ID不能为空',
            'shop_id.required'  =>  '店铺ID不能为空',
            'user_id.required'  =>  '店铺用户ID不能为空',
            'owner_id.integer'  =>  '盒子用户ID必须为整型',
            'shop_id.integer'   =>  '店铺ID必须为整型',
            'user_id.integer'   =>  '店铺用户ID必须为整型',
            'owner_id.exists'   =>  '盒子用户不存在',
            'shop_id.exists'    =>  '店铺不存在',
            'user_id.exists'    =>  '店铺用户不存在',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::SYSTEM_AREA_BIND_ERROR, $validator->errors()->first());
        }

        $owner_id   =   (int) $this->request->post('owner_id');
        $shop_id    =   (int) $this->request->post('shop_id');
        $user_id    =   (int) $this->request->post('user_id');

        [$flag, $result]    =   $this->service->areaBind($owner_id, $shop_id, $user_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::SYSTEM_AREA_BIND_ERROR, $result);
        }
    }

    /**
     * 添加盒子地区
     * @return \Illuminate\Http\JsonResponse
     */
    public function addArea()
    {
        $validator  =   Validator::make($this->request->post(), [
            'area_name'  =>  'required|string',
            'area_desc'  =>  'string',
        ], [
            'area_name.required' =>  '盒子地域不能为空',
            'area_name.string'   =>  '盒子地域必须为字符',
            'area_desc.string'   =>  '盒子地域描述必须为字符',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::SYSTEM_AREA_ADD_ERROR, $validator->errors()->first());
        }

        $area_name   =   $this->request->post('area_name');
        $area_desc   =   $this->request->post('area_desc', '');

        [$flag, $result]    =   $this->service->areaAdd($area_name, $area_desc);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::SYSTEM_AREA_ADD_ERROR, $result);
        }
    }

    /**
     * 盒子区域编辑
     * @return \Illuminate\Http\JsonResponse
     */
    public function editArea()
    {
        $validator  =   Validator::make($this->request->post(), [
            'area_name'  =>  'required|string',
            'area_desc'  =>  'string',
            'area_id'  =>  'required|integer|exists:mini_box_area,id',
        ], [
            'area_name.required' =>  '盒子地域不能为空',
            'area_name.string'   =>  '盒子地域必须为字符',
            'area_desc.string'   =>  '盒子地域描述必须为字符',
            'area_id.required'   =>  '盒子地域ID不能为空',
            'area_id.integer'    =>  '盒子地域ID必须为整型',
            'area_id.exists'     =>  '盒子地域不存在',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::SYSTEM_AREA_EDIT_ERROR, $validator->errors()->first());
        }

        $area_id     =   (int) $this->request->post('area_id');
        $area_name   =   $this->request->post('area_name');
        $area_desc   =   $this->request->post('area_desc', '');

        [$flag, $result]    =   $this->service->areaEdit($area_id, $area_name, $area_desc);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::SYSTEM_AREA_EDIT_ERROR, $result);
        }
    }

    /**
     * 删除盒子地区
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteArea()
    {
        $validator  =   Validator::make($this->request->post(), [
            'area_id'  =>  'required|integer|exists:mini_box_area,id',
        ], [
            'area_id.required' =>  '盒子地域ID不能为空',
            'area_id.integer'  =>  '盒子地域ID必须为整型',
            'area_id.exists'   =>  '盒子地域不存在',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::SYSTEM_AREA_DELETE_ERROR, $validator->errors()->first());
        }

        $area_id   =   (int) $this->request->post('area_id');

        [$flag, $result]    =   $this->service->areaDelete($area_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::SYSTEM_AREA_DELETE_ERROR, $result);
        }
    }

}
